package com.hc.dao;

import com.hc.bean.Order;
import com.hc.bean.OrderDetail;
import com.hc.bean.Pager;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface OrderDao {
    /*添加订单*/
    @Insert("insert into `order` values(#{orderId},#{userId},#{orderDate},#{orderStatus},#{orderFinishDate})")
    Integer addOrder(Order order);

    /*添加订单明细*/
    @Insert("insert into orderDetail values(null,#{orderId},#{productId},#{purchasePrice},#{purchaseNum})")
    Integer addOrderDetail(OrderDetail orderdetail);

    /*查询订单详情*/
    @Select("select * from orderDetail where orderId=#{orderId}")
    @Results({
            @Result(column = "productId", property = "productId"),
            @Result(column = "productId", property = "product", one = @One(select = "com.hc.dao.ProductDao.getProductById")),
    })
    List<OrderDetail> getOrderDetailByOrderId(int orderId);

    /*根据用户id查询订单*/
    @Select("select * from `order` where userId=#{userId} limit #{pager.firstLimitParam},#{pager.pageRows}")
    @Results({
            @Result(column = "orderId", property = "orderId"),
            @Result(column = "orderId", property = "orderDetailList", many = @Many(select = "com.hc.dao.OrderDao.getOrderDetailByOrderId")),
            @Result(column = "userId", property = "userId"),
            @Result(column = "userId", property = "user", one = @One(select = "com.hc.dao.UserDao.getUserByUid")),
    })
    List<Order> getOrderByUid(@Param("pager")Pager pager,@Param("userId")int userId);

    /*根据用户id查询订单总行数*/
    @Select("select * from `order` where userId=#{userId}")
    List<Order> getOrderCountByUid(int userId);
}
